package com.bl._01sync;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;


import java.nio.charset.StandardCharsets;
import java.util.List;

public class Consumer {

    public static void main(String[] args) throws Exception {
        // 1 创建消费者对象
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("sync_Consumer_group");
        // 2.连接mq
        consumer.setNamesrvAddr("localhost:9876");
        // 4.消费者订阅主题，表示此消费者消费哪个主题的消息，二级分类可以是*
        consumer.subscribe("sync","hello");
        // 5.注册监听器 topic 主题 subExpression
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt messageExt : list) {
                    System.out.println(new String(messageExt.getBody(), StandardCharsets.UTF_8));
                }
                // 标记该消息已经被成功消费
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        // 6 启动消费者
        consumer.start();
    }




}
